package com.xsw.common.utils;

import com.xsw.common.exception.ServiceException;
import org.apache.commons.lang3.ObjectUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author yjc
 * @create 2025-08-01 11:26
 */
public class CommonUtils {

    // 预编译正则表达式提升性能 [1,3](@ref)
    private static final Pattern PHONE_PATTERN = Pattern.compile("^1[3-9]\\d{9}$");

    /**
     * 工具类禁止实例化
     */
    private CommonUtils() {
        throw new ServiceException("工具类禁止实例化！");
    }

    /**
     * 验证手机号是否合法
     *
     * @param phone 待验证字符串
     * @return true-有效 | false-无效
     */
    public static boolean isValidPhone(String phone) {
        if (ObjectUtils.isEmpty(phone) || ObjectUtils.isEmpty(phone.trim())) return false;
        Matcher matcher = PHONE_PATTERN.matcher(phone);
        return matcher.matches();
    }
}
